package edu.pku.sei.modeler.editpolicies;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.BendpointRequest;
import edu.pku.sei.modeler.edit.policies.LinkBendpointEditPolicy;
import edu.pku.sei.modeler.editparts.ExecutionSpecificationEditpart;
import edu.pku.sei.modeler.editparts.LifelineEditpart;
import edu.pku.sei.modeler.editparts.MessageEditpart;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.uml.commands.sequence.SequenceReconnectSourceCommand;

public class MessageBendPointPolicy extends LinkBendpointEditPolicy {
	private MetaLink link = null;
	
	public MessageBendPointPolicy(MetaLink link) {
		super();
		this.link = link;
	}
	
	public Command getCommand(Request request)  {
		if (REQ_CREATE_BENDPOINT.equals(request.getType())) {
			int initHeight = link.getOffset() + link.getDelay() / 2;
			Point pt = ((BendpointRequest)request).getLocation().getCopy();
			MetaNode node = ((MetaNode)((MessageEditpart)getHost()).getSource().getModel());
			if (((MessageEditpart)getHost()).getSource() instanceof LifelineEditpart) {
				LifelineEditpart editpart = ((LifelineEditpart)((MessageEditpart)getHost()).getSource());
				editpart.getFigure().translateToRelative(pt);
			} else if (((MessageEditpart)getHost()).getSource() instanceof ExecutionSpecificationEditpart) {
				ExecutionSpecificationEditpart editpart = 
					((ExecutionSpecificationEditpart)((MessageEditpart)getHost()).getSource());
				editpart.getFigure().translateToRelative(pt);
			}
			int newOffset = pt.y - initHeight + link.getOffset() - node.getLocationY();
			return new SequenceReconnectSourceCommand(link, newOffset);
		}
		return null;
	}
	
	/**
	 * No feedback is shown when the bendpoint is created or moved.
	 * The anchors of the message can NOT be updated during dragging the bendpoint.
	 */
	public void showSourceFeedback(Request request) {
	
	}
}
